package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Company1;
import com.woniu.entity.Interview1;
import com.woniu.entity.Student1;
import com.woniu.pojo.Company;
import com.woniu.pojo.Interview;
import com.woniu.service.CompanyService;
import com.woniu.service.InterviewService;
import com.woniu.service.StudentService;
import com.woniu.utils.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("interview")
public class InterviewController {

    @Autowired
    private InterviewService interviewService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private CompanyService companyService;

    //展示页面跳转
    @RequestMapping("listJump")
    public String listJump(){
        return "WEB-INF/interview/list";
    }

    //增加页面跳转
    @RequestMapping("addJump")
    public String addJump(Model model){
        List<Student1> studentList = studentService.findAll(null);
        List<Company1> companyList = companyService.findAll(null);

        model.addAttribute("studentList",studentList);
        model.addAttribute("companyList",companyList);

        return "WEB-INF/interview/add";
    }

    //增加面试信息
    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object> add(Interview interview){
        System.out.println("=========================================================");
        System.out.println("interview = " +interview);
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            interviewService.add(interview);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    //按主键删除面试
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String,Object> deleteById(Integer viewId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            interviewService.deleteById(viewId);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    //查询一个面试信息
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String,Object> findOne(Integer viewId){
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            Interview1 interview = interviewService.findOne(viewId);

            map.put("interview",interview);
            map.put("result","ok");
        }catch(Exception e){
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    //跳转到修改
    @RequestMapping("updateJump")
    public String updateJump(Model model, Integer viewId){
        Interview1 interview = interviewService.findOne(viewId);
        List<Company1> companyList = companyService.findAll(null);
        model.addAttribute("companyList",companyList);
        model.addAttribute("interview",interview);

        return "WEB-INF/interview/update";
    }

    //修改面试信息
    @RequestMapping("update")
    @ResponseBody
    public Map<String, Object> update(Interview interview){
        System.out.println("========================================");
        System.out.println("interview = " + interview);
        System.out.println("========================================");
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            interviewService.updateById(interview);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    //面试信息查询分页
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize,Interview interview){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Interview1> interviewList = interviewService.findAll(interview);
            List<Student1> studentList = studentService.findAll(null);
            List<Company1> companyList = companyService.findAll(null);
            PageInfo<Interview1> pageInfo=new PageInfo<Interview1>(interviewList);
            map.put("pageInfo",pageInfo);
            map.put("interviewList",interviewList);
            map.put("studentList",studentList);
            map.put("companyList",companyList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    //面试信息批量删除
    @RequestMapping("deleteByExample")
    @ResponseBody
    public Map<String,Object> deleteByExample(Integer[] interviewIds){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            interviewService.deleteByExample(Arrays.asList(interviewIds));
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 学生查看自己的面试情况
     * @param studentId
     * @return
     */
    @RequestMapping("findViewResult")
    @ResponseBody
    public Map<String,Object> findViewResult(Integer studentId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            Interview interview = interviewService.findViewResult(studentId);
            map.put("interview",interview);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 班主任查看本班学员的面试情况
     * @param classId
     * @return
     */
    @RequestMapping("findClassViewResults")
    @ResponseBody
    public Map<String,Object> findClassViewResult(Integer classId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            List<Interview> interviewList = interviewService.findClassViewResults(classId);
            map.put("interviewList",interviewList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("exportInterview")
    public void exportInterview(HttpServletRequest request, HttpServletResponse response){
        List<Interview1> interviewList = interviewService.findAll(null);
        String[] title = {"ID","姓名","公司","详情","结果","评论","薪水"};
        String fileName = "interview.xls";
        String sheetName = "sheet1";
        String[][] content = new String[interviewList.size()][7];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            for (int i = 0;i < interviewList.size();i++){
                content[i][0] = String.valueOf(interviewList.get(i).getViewId());
                content[i][1] = interviewList.get(i).getStudentId().getStudentName();
                content[i][2] = interviewList.get(i).getCompanyId().getCompanyName();
                content[i][3] = interviewList.get(i).getViewDetail();
                content[i][4] = interviewList.get(i).getViewResult();
                content[i][5] = interviewList.get(i).getViewRemark();
                content[i][6] = interviewList.get(i).getViewSavvy();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName,title,content,null);
        try {
            this.setResponseHeader(response,fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            fileName = new String(fileName.getBytes(),"utf-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
    }

}
